package com.woniuxy.woniubookmanager.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.woniubookmanager.entity.Role;
import com.woniuxy.woniubookmanager.result.ResponseResult;
import com.woniuxy.woniubookmanager.service.RoleService;
import com.woniuxy.woniubookmanager.vo.RoleAuthority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wugd
 * @since 2022-11-29 04:03:13
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RoleService roleService ;

    @GetMapping("/roleList")
    public ResponseResult<Page<Role>> list(@RequestParam(value = "now",defaultValue = "1") Integer now ,
                                           @RequestParam(value = "size",defaultValue = "2")Integer size,
                                           String rname){
        ResponseResult<Page<Role>> result = null;
        try {
            Page<Role> page = roleService.getRoleByPage(now,size,rname);
            result = new ResponseResult<>(200,"ok",page);
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"查询失败!");
            e.printStackTrace();
        }

        return result;
    }

    @PostMapping("/addRole")
    public ResponseResult<Void> addRole(@RequestBody Role role){
        ResponseResult<Void> result = null;
        try {
            role.setInsertTime(new Date());
            roleService.save(role);
            result = new ResponseResult<>(200,"ok");
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"添加失败!");
            e.printStackTrace();
        }

        return result ;
    }

    @PutMapping("/modifyRole")
    public ResponseResult<Void> modifyRole(@RequestBody Role role){
        ResponseResult<Void> result = null;
        try {
            role.setUpdateTime(new Date());
            roleService.updateById(role);
            result = new ResponseResult<>(200,"ok");
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"修改失败!");
            e.printStackTrace();
        }

        return result ;
    }

    @DeleteMapping("/del/{id}")
    public ResponseResult<Void> del(@PathVariable("id") Long id){
        ResponseResult<Void> result = null;
        try {
            roleService.removeRoleById(id);
            result = new ResponseResult<>(200,"ok");
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"删除失败!");
            e.printStackTrace();
        }
        return result ;
    }

    @PutMapping("/roleAuthority")
    public ResponseResult<Void> roleAuthority(@RequestBody RoleAuthority roleAuthority){
        ResponseResult<Void> result = null;
        try {
            roleService.roleAuthority(roleAuthority);
            result = new ResponseResult<>(200,"ok");
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"授权失败!");
            e.printStackTrace();
        }
        return result ;
    }

    @GetMapping("/getRoleIdByMid/{id}")
    public ResponseResult<Long[]> getRoleIdByMid(@PathVariable("id") Long id){
        ResponseResult<Long[]> result = null;
        try {
            Long[] arr = roleService.getRoleIdByMid(id);
            result = new ResponseResult<>(200,"ok",arr);
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"查询失败!");
            e.printStackTrace();
        }
        return result ;
    }

    @GetMapping("/getAllRoles")
    public ResponseResult<List<Role>> getAllRoles(){
        ResponseResult<List<Role>> result = null;
        try {
            List<Role> list = roleService.list();
            result = new ResponseResult<>(200,"ok",list);
        }catch (RuntimeException e) {
            result = new ResponseResult<>(201,"查询失败!");
            e.printStackTrace();
        }

        return result;
    }
}

